package com.atech.ebayadmin.common.tablibrary;

import java.io.IOException;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.atech.ebayadmin.system.util.FilterUtil;
import com.atech.ebayadmin.system.vo.UserVO;


public class PermissionTag extends BodyTagSupport {
	private static final long   serialVersionUID = -6405339272421904981L;
    private static Logger  log = LogManager.getLogger(PermissionTag.class);
    private String permStr;
	public String getPermStr() {
		return permStr;
	}
	public void setPermStr(String permStr) {
		this.permStr = permStr;
	}
    
    public void init() throws IllegalStateException {
    	log.info("开始初始化权限自定义标签");
    }

  @Override
  public void setPageContext(PageContext pageContext) {
      super.setPageContext(pageContext);
      try {
          this.init();
      }
      catch (IllegalStateException e) {
          log.error(e.getMessage(), e);
      }
  }

  @Override
  public int doStartTag() throws JspException {
      HttpSession session = this.pageContext.getSession();
      log.info("访问的权限标签为："+ getPermStr());
      try {
          if (null == session) {
              this.pageContext.getOut().print("取不到会话");
              return PermissionTag.SKIP_PAGE;
          }
          UserVO user = (UserVO)session.getAttribute("User");
          if (null == user) {
              this.pageContext.getOut().print("取不到用户的会话ϵͳ");
              return PermissionTag.SKIP_PAGE;
          }else{
        	  if(FilterUtil.getInstance().isHasPermission(user.getRoleId(), getPermStr())){
        		  log.info("用户具有权限显示菜单"+getPermStr());
        		  return PermissionTag.EVAL_BODY_INCLUDE;
        	  }
              
          }
      }
      catch (NullPointerException npe) {
          log.error(npe.getMessage(), npe);
          return PermissionTag.SKIP_BODY;
      }
      catch (IOException e) {
          log.error(e.getMessage(), e);
          return PermissionTag.SKIP_BODY;
      }
      return PermissionTag.SKIP_PAGE;
  }

}
